Deployment

Part 2, Chapter 9


With the routes up and tested, let's get this app deployed to Heroku!


Gunicorn

To use Gunicorn, a production-grade WSGI server, first add the dependency to the requirements.txt file:

gunicorn==20.1.0

Why Gunicorn and Uvicorn? To get the best of concurrency and parallelism. Gunicorn manages multiple, concurrent Uvicorn processes.

Add a new Dockerfile called Dockerfile.prod:

# pull official base image
FROM python:3.10.1-slim-buster

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup --system app && adduser --system --group app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV ENVIRONMENT prod
ENV TESTING 0

# install system dependencies
RUN apt-get update \
  && apt-get -y install netcat gcc postgresql \
  && apt-get clean

# install python dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN pip install "uvicorn[standard]==0.16.0"

# add app
COPY . .

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run gunicorn
CMD gunicorn --bind 0.0.0.0:$PORT app.main:app -k uvicorn.workers.UvicornWorker

What's different here from the original Dockerfile?

Well, we added a CMD to run Gunicorn (with a uvicorn worker class) and configured two new environment variables:

ENV ENVIRONMENT prod
ENV TESTING 0

We also created and switched to a non-root user, which is recommended by Heroku.

Finally, take note of the $PORT environment variable in the Gunicorn command. Essentially, our web app must be listening on a particular port specified by the $PORT environment variable, which is supplied by Heroku.

Heroku

Sign up for a Heroku account (if you don’t already have one), and then install the Heroku CLI (if you haven't already done so).

Create a new app:

$ heroku create
Creating app... done, ⬢ sleepy-thicket-77414
https://sleepy-thicket-77414.herokuapp.com/ | https://git.heroku.com/sleepy-thicket-77414.git

Log in to the Heroku Container Registry:

$ heroku container:login

Provision a new Postgres database with the hobby-dev plan:

$ heroku addons:create heroku-postgresql:hobby-dev --app sleepy-thicket-77414

Build the production image and tag it with the following format:

registry.heroku.com/<app>/<process-type>

Make sure to replace <app> with the name of the Heroku app that you just created and <process-type> with web since this will be a web dyno.

For example:

$ docker build -f project/Dockerfile.prod -t registry.heroku.com/sleepy-thicket-77414/web ./project

To test locally, spin up the container:

$ docker run --name fastapi-tdd -e PORT=8765 -e DATABASE_URL=sqlite://sqlite.db -p 5003:8765 registry.heroku.com/sleepy-thicket-77414/web:latest

You should see something similar to:

[2021-07-06 12:22:08 +0000] [8] [INFO]  Starting gunicorn 20.1.0
[2021-07-06 12:22:08 +0000] [8] [INFO]  Listening at: http://0.0.0.0:8765 (8)
[2021-07-06 12:22:08 +0000] [8] [INFO]  Using worker: uvicorn.workers.UvicornWorker
[2021-07-06 12:22:08 +0000] [10] [INFO] Booting worker with pid: 10
[2021-07-06 12:22:08 +0000] [10] [INFO] Started server process [10]
[2021-07-06 12:22:08 +0000] [10] [INFO] Waiting for application startup.
[2021-07-06 12:22:08 +0000] [10] [INFO] Application startup complete.

Navigate to http://localhost:5003/ping/.

You should see:

{
  "ping": "pong!",
  "environment": "prod",
  "testing": false
}

Bring down the container once done:

$ docker rm fastapi-tdd -f

Push the image to the registry:

$ docker push registry.heroku.com/sleepy-thicket-77414/web:latest

Release the image:

$ heroku container:release web --app sleepy-thicket-77414

Again, replace sleepy-thicket-77414 in each of the above commands with the name of your app.

This will run the container. You should be able to view the app at https://APP_NAME.herokuapp.com/ping/.

Apply the migrations:

$ heroku run aerich upgrade --app sleepy-thicket-77414

How about the summaries endpoints? Do they work?

Endpoint HTTP Method CRUD Method Result
/summaries GET READ get all summaries
/summaries/:id GET READ get single summary
/summaries POST CREATE add a summary

Try adding a new summary with HTTPie:

$ http --json POST https://sleepy-thicket-77414.herokuapp.com/summaries/ url=https://testdriven.io

You should see:

HTTP/1.1 201 Created
Connection: keep-alive
Content-Length: 38
Content-Type: application/json
Date: Mon, 27 Dec 2021 15:32:57 GMT
Server: uvicorn
Via: 1.1 vegur

{
    "id": 1,
    "url": "https://testdriven.io"
}

Ensure https://APP_NAME.herokuapp.com/docs works as well.




✓ Mark as Completed